package com.flute.icrawler.app.processor.check;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.icrawler.app.entity.CrawlResult;
import com.flute.icrawler.app.processor.AbstractProcessor;
import com.flute.icrawler.app.service.CrawlService;
import com.flute.icrawler.app.util.ParameterKey;
import com.flute.icrawler.framework.framework.CrawlUrl;
import com.flute.icrawler.framework.processor.result.FilterProcessorResult;
import com.flute.icrawler.framework.processor.result.ResultParameter;

/**
 * 检查所有相关配置 深度 重试次数等
 * 
 * @author jiangbo
 * 
 */
public class CheckConfigProcessor extends AbstractProcessor {

	private String filterReg = "";

	private String matchReg = "";

	private int deep = 0;

	private static final Logger LOGGER = LoggerFactory
			.getLogger(CheckConfigProcessor.class);

	public void process(CrawlUrl curi) {

		LOGGER.debug("CheckConfigProcessor:{}", curi.getUrl() + "|DEEP="
				+ curi.getDeep());

		CrawlResult result = new CrawlResult();
		ResultParameter parameter = new ResultParameter();
		parameter.setName(ParameterKey.RESULT_CRAWL);
		parameter.setValue(result);
		curi.registerResultParameter(parameter);

		// // 链接字符的合法性
		// if (!isUrl(curi.getUrl())) {
		// String errorMsg = "is not an url";
		// curi.registerProcessorResult(new FetchStatusProcessorResult(
		// FetchStatus.Filter, true));
		// LOGGER.info(curi.getUrl() + errorMsg);
		// return;
		// }
		// 深度
		if (deep != 0 && curi.getDeep() > deep) {
			String errorMsg = "getDepth=" + curi.getDeep() + ";MaxDepth="
					+ deep;
			LOGGER.warn(errorMsg + " url = " + curi.getUrl());
			FilterProcessorResult filterProcessorResult = new FilterProcessorResult();
			curi.registerProcessorResult(filterProcessorResult);
			return;
		}

		if (!CrawlService.match(curi.getUrl(), filterReg, matchReg)) {
			LOGGER.warn("find unmatch url = " + curi.getUrl());
			FilterProcessorResult filterProcessorResult = new FilterProcessorResult();
			curi.registerProcessorResult(filterProcessorResult);
			return;
		}

	}

	/**
	 * URL检查
	 * 
	 * 
	 * @param pInput
	 *            要检查的字符串
	 * @return boolean 返回检查结果
	 */
	// public boolean isUrl(String pInput) {
	// if (pInput == null) {
	// return false;
	// }
	//
	// String regEx = "http://(\\w+\\.)+\\w+/?[^:]*";
	//
	// Pattern p = Pattern.compile(regEx);
	// Matcher matcher = p.matcher(pInput);
	// return matcher.matches();
	//
	// }

	public static void main(String[] args) {

		CrawlUrl curi = new CrawlUrl(null, "http://music.douban.com/tag/J-Pop　");
		// System.out.println("match:"
		// + new CheckConfigProcessor().acceptMatch(
		// ".*[^sina]\\.com\\.cn", "163"));

		// / .*\\.[^sina]{4}\\.com\\.cn\.*

		Pattern pattern = Pattern.compile("\\.[^sina]\\.com\\.cn");
		Matcher matcher = pattern.matcher("http://www.163.com.cn");

		System.out.println("match2:" + matcher.find());

		new CheckConfigProcessor().process(curi);

		System.out.println(new CheckConfigProcessor().getName());

		// System.out.println(new CheckConfigProcessor()
		// .isUrl("http://music.douban.com/降低"));
		// System.out.println(new CheckConfigProcessor()
		// .isUrl("http://music.douban.com/uuu"));
		// System.out.println("http://music.douban.com/tag/J-Pop　".trim());
	}
}
